package com.yg.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.code.kaptcha.Constants;
import com.yg.pojo.TUser;
import com.yg.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kotori
 * @since 2022-11-18
 */
@Controller
@RequestMapping("/user")
public class TUserController {
    @Autowired
    private TUserService userService;

    @PostMapping("/login")
    public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session, ModelAndView modelAndView) {
        modelAndView.addObject("username", username);
        modelAndView.addObject("password", password);
        if (userService.login(username, password)) {
            session.setAttribute("loginUser", username);
            modelAndView.setViewName("user/login_success");
            return modelAndView;
        } else {
            modelAndView.addObject("msg", "用户名或密码错误!");
            modelAndView.setViewName("user/login");
            return modelAndView;
        }
    }

    @PostMapping("/regist")
    public ModelAndView regist(ModelAndView modelAndView, HttpSession session, @RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("code") String code, @RequestParam("email") String email) {
        String rightcode = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        modelAndView.addObject("username", username);
        modelAndView.addObject("email", email);
        if (!rightcode.equals(code)) {
            modelAndView.addObject("msg", "验证码错误！");
            modelAndView.setViewName("/user/regist");
        } else {
            LambdaQueryWrapper<TUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TUser::getUsername, username);
            System.out.println(userService.getOne(lambdaQueryWrapper));
            if (userService.getOne(lambdaQueryWrapper) == null) {
                TUser user = new TUser(null, username, password, email);
                boolean save = userService.save(user);
                if (save) {
                    modelAndView.setViewName("/user/regist_success");
                } else {
                    modelAndView.setViewName("/user/regist");
                    modelAndView.addObject("msg", "系统故障，暂时无法注册");
                }
            } else {
                modelAndView.addObject("msg", "用户名已存在！");
                modelAndView.setViewName("/user/regist");
            }

        }
        return modelAndView;
    }

    @GetMapping("/logout")
    public void logout(HttpServletResponse resp, HttpSession session) throws IOException {
        session.removeAttribute("loginUser");
        resp.sendRedirect("/index");
    }
}

